package com.hgw.easyexcel3_demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.ImageData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.ListUtils;
import com.hgw.easyexcel3_demo.domain.ImageDemoData;
import com.hgw.easyexcel3_demo.handler.ImageCellWriteHandler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author Linhuibq-YanAn
 * @date 2022/10/20 10:24
 */
@SpringBootTest
public class imageDataWrite {

    private final String PATH = "/Users/gwh/Documents/data/demo/easy-excel-demo/easyexcel3_demo/src/main/resources/warehouse/";


    @Test
    public void imageWrite() throws MalformedURLException {
        String fileName = PATH + "imageWrite" + System.currentTimeMillis() + ".xlsx";

        String imagePath = PATH + "img.png";

        try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) {
            List<ImageDemoData> list = ListUtils.newArrayList();
            ImageDemoData imageDemoData = new ImageDemoData();
            list.add(imageDemoData);
            // 放入五种类型的图片 实际使用只要选一种即可
            imageDemoData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath)));
            imageDemoData.setFile(new File(imagePath));
            imageDemoData.setString(imagePath);
            imageDemoData.setInputStream(inputStream);
            imageDemoData.setUrl(new URL(
                    "https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark"));
            imageDemoData.setName("HGW");

            WriteCellData<Void> writeCellData = new WriteCellData<>();
            imageDemoData.setWriteCellDataFile(writeCellData);

            List<ImageData> imageDataList = new ArrayList<>();
            // 第一张图
            ImageData imageData = new ImageData();
            imageDataList.add(imageData);
            writeCellData.setImageDataList(imageDataList);
            imageData.setImage(getUrlFileData("https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark"));
            imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG);
  /*          imageData.setRight(250);
            imageData.setTop(5);
            imageData.setBottom(5);
            imageData.setLeft(5);*/

            // 第二张图
            imageData = new ImageData();
            imageDataList.add(imageData);
            writeCellData.setImageDataList(imageDataList);
            imageData.setImage(getUrlFileData("https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark"));
            imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG);
     /*       imageData.setLeft(70);
            imageData.setRight(150);
            imageData.setTop(5);
            imageData.setRight(5);
            imageData.setBottom(5);*/

            // 设置图片的位置 假设 现在目标 是 覆盖 当前单元格 和当前单元格右边的单元格
            // 起点相对于当前单元格为0 当然可以不写
/*            imageData.setRelativeFirstRowIndex(0);
            imageData.setRelativeFirstColumnIndex(0);
            imageData.setRelativeLastRowIndex(0);*/

            // 前面3个可以不写  下面这个需要写 也就是 结尾 需要相对当前单元格 往右移动一格
            // 也就是说 这个图片会覆盖当前单元格和 后面的那一格
//            imageData.setRelativeLastColumnIndex(1);

            EasyExcel.write(fileName,ImageDemoData.class)
                    .registerWriteHandler(new ImageCellWriteHandler())
                    .sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static byte[] getUrlFileData(String fileUrl) throws Exception {
        URL url = new URL(fileUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.connect();
        InputStream cin = httpConn.getInputStream();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = cin.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        cin.close();
        byte[] fileData = outStream.toByteArray();
        outStream.close();
        return fileData;
    }
}
